package com.example.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.logging.Logger;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class util extends AppCompatActivity {
    public void setWebImage(String webSrc, ImageView imageBox) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request builder = new Request.Builder().url(webSrc).build();
        okHttpClient.newCall(builder).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageBox.setImageResource(R.drawable.icon_la);
                    }
                });
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                InputStream inputStream = response.body().byteStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageBox.setImageBitmap(bitmap);
                    }
                });
            }
        });
    }
    public void POST(String url, String token, String ocntent, Handler mHandler) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestJosn = RequestBody.create(MediaType.parse("application/json"), ocntent);
        Request request = new Request.Builder().url(url).addHeader("token", token).post(requestJosn).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.i("aaa", "ccc");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Message message = new Message();
                message.obj = response.body().string();
                mHandler.sendMessage(message);
            }
        });

    }

    public void GET(String url, String token, Handler ehandler) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).addHeader("token", token).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.i("bbb", "ddd");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Message messagesser = new Message();
                messagesser.obj = response.body().string();
                ehandler.sendMessage(messagesser);
            }
        });
    }

    public void saveLoginData(Context context, String username, String password) {
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences("LoginData", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("username", username);
            editor.putString("password", password);
            editor.apply();
        } catch (NullPointerException e) {
            Log.i("TAG", "111111111");
        }
    }

    public String[] getLoginData(Context context) {
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences("LoginData", Context.MODE_PRIVATE);
            return new String[]{sharedPreferences.getString("username", ""), sharedPreferences.getString("password", "")};
        } catch (NullPointerException e) {
            return new String[]{"", ""};
        }
    }


}
